package cn.js.domain.entity;


import cn.js.domain.User;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private User user;

    /**
     * ++++++++++++++++++++++++++++++++ 权限 ++++++++++++++++++++++++++++
     */
    //传入的参数权限列表

    private List<String> jurisdictions;


    public LoginUser(User user, List<String> jurisdictions) {
        this.user = user;
        this.jurisdictions = jurisdictions;
    }

    //自定义一个权限列表集合
    @JSONField(serialize = false) //不加的话，之后json序列化转不回来
    List<SimpleGrantedAuthority> authorities;//GrantedAuthority 的子类


    //用来返回权限信息
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities != null) return authorities;
        authorities = new ArrayList<>();

        for (String s : jurisdictions) {
            SimpleGrantedAuthority jurisdiction = new SimpleGrantedAuthority(s);
            authorities.add(jurisdiction);
        }

        return authorities;
    }


    /**
     * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     *
     */

    //获取密码
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    //获取用户名
    @Override
    public String getUsername() {
        return user.getUserName();
    }

    //判断账号是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //判断账号是否没有锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //判断账号是否没有超时
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //判断账号是否可用
    @Override
    public boolean isEnabled() {
        return true;
    }
}
